package org.jeecg.modules.monitoring.j_monitoring.controller;

import java.util.Arrays;
import java.util.Date;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.monitoring.j_monitoring.entity.MGlInformationSystem;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MGlInformationSystemMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlInformationSystemService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 信息系统
 * @Author: xiaoyq
 * @Date:   2023-02-23
 * @Version: V1.0
 */
@Api(tags="信息系统")
@RestController
@RequestMapping("/j_monitoring/mGlInformationSystem")
@Slf4j
public class MGlInformationSystemController extends JeecgController<MGlInformationSystem, IMGlInformationSystemService> {
	@Autowired
	private MGlInformationSystemMapper mGlInformationSystemMapper;
	/**
	 * 分页列表查询
	 *
	 * @param mGlInformationSystem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "信息系统-分页列表查询")
	@RequiresPermissions("信息系统:查询")
	@ApiOperation(value="信息系统-分页列表查询", notes="信息系统-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(MGlInformationSystem mGlInformationSystem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Result<Page<MGlInformationSystem>> result=new Result<>();
		Page<MGlInformationSystem> pagelist = new Page(pageNo, pageSize);
		QueryWrapper<MGlInformationSystem> queryWrapper = QueryGenerator.initQueryWrapper(mGlInformationSystem, req.getParameterMap());
		pagelist=mGlInformationSystemMapper.getPageList(pagelist,queryWrapper);
		result.setCode(200);
		result.setSuccess(true);
		result.setResult(pagelist);
		return result;
	}

	 /**
	  * 设备地图查询
	  */
	 //@AutoLog(value = "虚拟机(管理)-设备地图查询")
	 @RequiresPermissions("信息系统:查询")
	 @ApiOperation(value="虚拟机(管理)-设备地图查询", notes="虚拟机(管理)-设备地图查询")
	 @GetMapping(value = "/getDeviceMap")
	 public Result<?> getDeviceMap(
			 @RequestParam(name="id",required=true) String id,
			 HttpServletRequest req) {
		 MGlInformationSystem entity = this.service.getById(id);
		 JSONObject result=new JSONObject();

		 JSONArray nodes = new JSONArray();
		 JSONArray edges = new JSONArray();
		 result.put("nodes",nodes);
		 result.put("edges",edges);
		 if(entity==null){
			 return Result.OK(result);
		 }

		 int startX = 0, startY =0;

		 JSONObject child = this.service.getDeviceMapById(nodes,edges,startX,startY,id);

		 return Result.OK(result);
	 }

	/**
	 *   添加
	 *
	 * @param mGlInformationSystem
	 * @return
	 */
	@AutoLog(value = "信息系统-添加")
	@ApiOperation(value="信息系统-添加", notes="信息系统-添加")
	@RequiresPermissions("信息系统:添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody MGlInformationSystem mGlInformationSystem) {
		mGlInformationSystem.setCreateTime(new Date());
		mGlInformationSystem.setApplicationNo(service.getNewNo());
		service.save(mGlInformationSystem);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param mGlInformationSystem
	 * @return
	 */
	@AutoLog(value = "信息系统-编辑")
	@ApiOperation(value="信息系统-编辑", notes="信息系统-编辑")
	@RequiresPermissions("信息系统:编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody MGlInformationSystem mGlInformationSystem) {
		service.updateById(mGlInformationSystem);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "信息系统-通过id删除")
	@ApiOperation(value="信息系统-通过id删除", notes="信息系统-通过id删除")
	@RequiresPermissions("信息系统:删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		service.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "信息系统-批量删除")
	@ApiOperation(value="信息系统-批量删除", notes="信息系统-批量删除")
	@RequiresPermissions("信息系统:批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.service.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "信息系统-通过id查询")
	@ApiOperation(value="信息系统-通过id查询", notes="信息系统-通过id查询")
	@GetMapping(value = "/queryById")
	@RequiresPermissions("信息系统:查询")
	public Result<MGlInformationSystem> queryById(@RequestParam(name="id",required=true) String id) {
		MGlInformationSystem mGlInformationSystem = service.getById(id);
		if(mGlInformationSystem==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(mGlInformationSystem);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param mGlInformationSystem
    */
	@RequiresPermissions("信息系统:导出")
    @RequestMapping(value = "/exportXls", method = RequestMethod.GET)
	@ApiOperation(value="信息系统-导出", notes="信息系统-导出")
    public ModelAndView exportXls(HttpServletRequest request, MGlInformationSystem mGlInformationSystem) {
        return super.exportXls(request, mGlInformationSystem, MGlInformationSystem.class, "信息系统");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
	@RequiresPermissions("信息系统:导入")
	@ApiOperation(value="信息系统-导入", notes="信息系统-导入")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MGlInformationSystem.class);
    }

}
